<? include ("../init.php") ?>
<?
  $name = $_POST['name'];
  
  $sql = "INSERT INTO hs_category(c_name) VALUES('$name')";
  $db->execute($sql);
  $aid = $db->lastInsertedId();
  
  // ---------- UPLOADS Image ----------
  $handle = new Upload($_FILES['image1']);
  
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->image_resize = true;
        $handle->image_ratio_fill = true;
        $handle->image_x = 239;
        $handle->image_y = 312;
        $handle->Process(APPPATH.'/images/products/category/'.$aid);
        
        if ($handle->processed)
        {
           $sql = "UPDATE hs_category SET c_img1='$handle->file_dst_name' WHERE c_id=$aid";
           if(!mysql_query($sql))
           {
                $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
                unlink(APPPATH."/images/products/category/$aid/".$handle->file_dst_name);
           }
        }
        else
        {
            $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
        }
        
        $handle->clean();
    }
    else
    {
        $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
    }
  }
  
  $handle = new Upload($_FILES['image2']);
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->image_resize = true;
        $handle->image_ratio_fill = true;
        $handle->image_x = 128;
        $handle->image_y = 37;
        $handle->Process(APPPATH.'/images/products/category/'.$aid);
        
        if ($handle->processed)
        {
           $sql = "UPDATE hs_category SET c_img2='$handle->file_dst_name' WHERE c_id=$aid";
           if(!mysql_query($sql))
           {
                $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
                unlink(APPPATH."/images/products/category/$aid/".$handle->file_dst_name);
           }
        }
        else
        {
            $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
        }
        
        $handle->clean();
    }
    else
    {
        $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
    }
  }
  header("Location:index.php");
?>